package cn.iocoder.yudao.module.vlog.info.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.api.product.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.vlog.info.convert.VLogInfoConvert;
import cn.iocoder.yudao.module.vlog.info.dal.dataobject.VLogInfoDO;
import cn.iocoder.yudao.module.vlog.info.service.VLogInfoService;
import cn.iocoder.yudao.module.vlog.info.vo.VLogInfoPageReqVO;
import cn.iocoder.yudao.module.vlog.info.vo.VLogInfoRespVO;
import cn.iocoder.yudao.module.vlog.info.vo.VLogInfoStatusUpdateReqVO;
import cn.iocoder.yudao.module.vlog.info.vo.VLogInfoUpdateReqVO;
import cn.iocoder.yudao.module.vlog.product.service.VLogProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "管理后台 - 视频信息")
@RestController
@RequestMapping("/vlog/info")
@Validated
public class VLogInfoController {

    @Resource
    private VLogInfoService vLogInfoService;
    @Resource
    private VLogProductService vLogProductService;
    @Resource
    private MemberUserApi memberUserApi;
//    @Resource
//    private ShopInfoApi shopDetailApi;

    @PostMapping("/update")
    @Operation(summary = "更新视频信息")
    @PreAuthorize("@ss.hasPermission('vlog:info:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody VLogInfoUpdateReqVO updateReqVO) {
        vLogInfoService.updateInfo(updateReqVO);
        return success(true);
    }

    @PostMapping("/update-status")
    @Operation(summary = "更新视频状态")
    @PreAuthorize("@ss.hasPermission('vlog:info:update')")
    public CommonResult<Boolean> updateStatus(@Valid @RequestBody VLogInfoStatusUpdateReqVO updateReqVO) {
        vLogInfoService.updateStatus(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除视频信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vlog:info:delete')")
    public CommonResult<Boolean> deleteInfo(@RequestParam("id") Long id) {
        vLogInfoService.deleteInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得视频信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vlog:info:query')")
    public CommonResult<VLogInfoRespVO> getInfo(@RequestParam("id") Long id) {
        VLogInfoDO info = vLogInfoService.getInfo(id);
        return success(BeanUtils.toBean(info, VLogInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得视频信息分页")
    @PreAuthorize("@ss.hasPermission('vlog:info:query')")
    public CommonResult<PageResult<VLogInfoRespVO>> getInfoPage(@Valid VLogInfoPageReqVO pageReqVO) {
        PageResult<VLogInfoDO> pageResult = vLogInfoService.getInfoPage(pageReqVO);
        List<VLogInfoDO> list = pageResult.getList();
        if (CollUtil.isEmpty(list)) {
            return success(PageResult.empty());
        }
        Set<Long> userIds = convertSet(list, VLogInfoDO::getUserId);
        Map<Long, UserSimpleVO> users = memberUserApi.getUserSimpleMap(userIds);
        Set<Long> vlogIds = convertSet(pageResult.getList(), VLogInfoDO::getId);
        Map<Long, Collection<ProductSpuRespDTO>> spus = vLogProductService.getProductSpuMap(vlogIds);
        return success(VLogInfoConvert.INSTANCE.convertPage(pageResult, users, spus));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出视频信息 Excel")
    @PreAuthorize("@ss.hasPermission('vlog:info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInfoExcel(@Valid VLogInfoPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VLogInfoDO> list = vLogInfoService.getInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "视频信息.xls", "数据", VLogInfoRespVO.class,
                BeanUtils.toBean(list, VLogInfoRespVO.class));
    }

}
